﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuickApp.Core;
using Resto.CostControl.Core;
using EfSearchModel.Model;
using QuickApp.MVCControl.FlexiGrid;
using QuickApp.MVCControl;
using QuickApp.MVCControl.Form;

namespace Resto.CostControl.Web.Controllers
{
    public class MaterialController : Controller
    {
        private readonly IRepository<Material> _Repo;
        private readonly IUnitOfWork _UnitOfWork;

        public MaterialController(IRepository<Material> repo, UnitOfWork unitOfWork)
        {
            _Repo = repo;
            _UnitOfWork = unitOfWork;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult List(DWZPageInfo pager, DWZQueryModel where)
        {
            var list = _Repo.GetPageable(pager.ToPageInfo(), where);
            var fdata = pager.ToFlexiGridData<Material>(list.Page.ToList(), list.Total);
            return Json(fdata);
        }

        public ActionResult Edit(string Id)
        {
            Material data = null;
            if (!string.IsNullOrWhiteSpace(Id))
            {
                data = _Repo.Find(Id);
            }
            else
            {
                data = new Material();
                data.Type = "RawMaterial";
            }
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(Material data)
        {
            JsonReturnMessages msg = new JsonReturnMessages();
            try
            {
                if (ModelState.IsValid)
                {
                    if (string.IsNullOrWhiteSpace(data.Id))
                    {
                        data.Id = Guid.NewGuid().ToString();
                        _Repo.Insert(data);
                    }
                    else
                    {
                        var oldData = _Repo.Find(data.Id);
                        TryUpdateModel(oldData);
                    }

                    _UnitOfWork.Commit();
                    msg.IsSuccess = true;
                    msg.Msg = "操作成功";
                }
            }
            catch (Exception ex)
            {
                msg.IsSuccess = false;
                msg.Msg = "操作失败：" + ex.Message;
            }
            return Json(msg);
        }

        [HttpPost]
        public JsonResult Delete(string Id)
        {
            JsonReturnMessages msg = new JsonReturnMessages();
            if (!string.IsNullOrEmpty(Id))
            {
                try
                {
                    _Repo.Delete(Id);
                    _UnitOfWork.Commit();
                    msg.IsSuccess = true;
                    msg.Msg = "操作成功";
                }
                catch (Exception ex)
                {
                    msg.IsSuccess = false;
                    msg.Msg = "操作失败:" + ex.Message;
                }
            }
            else
            {
                msg.IsSuccess = false;
                msg.Msg = "参数错误";
            }
            return Json(msg);
        }

        //public ActionResult LookupMaterial(DWZPageInfo pageInfo, QueryModel where)
        //{
        //    var materials = _Repo.GetPageable(pageInfo, where);

        //    ViewBag.Total = materials.Total;
        //    ViewBag.PageNum = pageInfo.PageIndex + 1;
        //    ViewBag.NumPerPage = pageInfo.PageSize;
        //    ViewBag.OrderField = pageInfo.SortName;
        //    ViewBag.OrderDirection = pageInfo.SortOrder;
        //    ViewBag.Keywords = pageInfo.Keywords;
        //    ViewBag.SearchData = pageInfo.SearchData;

        //    return View(materials.Page);
        //}
    }

    /// <summary>
    /// 每页显示数量的实体
    /// </summary>
    public class PageSize
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }

    /// <summary>
    /// Ajax表单返回结果
    /// </summary>
    public class FormReturnJson
    {
        public string statusCode { get; set; }
        public string message { get; set; }
        public string navTabId { get; set; }
        public string callbackType { get; set; }
        public string forwardUrl { get; set; }
    }
}
